package com.toommi.refuse.controller;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.Building;
import com.toommi.refuse.entity.Household;
import com.toommi.refuse.entity.Neighbourhood;
import com.toommi.refuse.service.NeighbourhoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * ..
 * 小区信息
 *
 * @author yangg
 * @since 2019-07-25 11:20
 */
@RestController
@RequestMapping(value = "")
@Slf4j
@AllArgsConstructor
@PermissionController(groupCode = "admin_neighbourhood", groupName = "小区管理")
@Api(tags = "小区、楼栋、门牌号信息")
public class NeighbourhoodController {

    private NeighbourhoodService neighbourhoodService;

    /**
     * 新增小区信息
     *
     * @param neighbourhood 小区信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增小区信息", notes = "新增小区信息")
    @ApiImplicitParam(name = "neighbourhood", value = "小区信息实体", required = true, dataType = "Neighbourhood")
    @RequiresPermissions("admin:neighbourhood:add")
    @PermissionReport(value = "admin:neighbourhood:add", name = "新增小区", group = "admin_neighbourhood")
    @RequestMapping(value = "/admin/neighbourhood", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse addNeighbourhood(@RequestBody Neighbourhood neighbourhood) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            neighbourhoodService.addNeighbourhood(neighbourhood);
        } catch (Exception e) {
            log.error("add neighbourhood error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除小区信息
     *
     * @param id 小区id
     * @return 删除结果
     */
    @ApiOperation(value = "删除小区信息", notes = "删除小区信息")
    @ApiImplicitParam(name = "id", value = "小区信息id", required = true, dataType = "Long")
    @RequiresPermissions("admin:neighbourhood:delete")
    @PermissionReport(value = "admin:neighbourhood:delete", name = "删除小区", group = "admin_neighbourhood")
    @RequestMapping(value = "/admin/neighbourhood/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public CommonResponse deleteNeighbourhood(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            neighbourhoodService.deleteNeighbourhood(id);
        } catch (Exception e) {
            log.error("delete neighbourhood error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;

    }

    /**
     * 根据街道id查询小区列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param street   街道id
     * @return 小区信息分页对象
     */
    @ApiOperation(value = "根据街道id查询小区信息", notes = "根据街道id查询小区信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "street", value = "街道id", required = true, dataType = "String")
    })
    @RequestMapping(value = "/api/street/neighbourhoods", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse findNeighbourhoods(int pageSize, int pageNum, String street) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(neighbourhoodService.findByStreet(pageNum, pageSize, street).getList());
        } catch (Exception e) {
            log.error("get neighbourhoods error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
    /**
     * 后台获取小区列表
     * @return 获取回收员列表
     */
    @ApiOperation(value = "后台获取小区列表", notes = "后台获取小区列表")

    @GetMapping(value = "/api/htstreet/neighbourhoods", produces = "application/json;charset=UTF-8")
    public Object getNeighbourhoodslist(HttpServletRequest request) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return  neighbourhoodService.getNeighbourhoodslist(request);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }

    /**
     * 后台获取楼栋列表
     * @return 获取回收员列表
     */
    @ApiOperation(value = "后台获取楼栋列表", notes = "后台获取楼栋列表")

    @GetMapping(value = "/api/buildlist", produces = "application/json;charset=UTF-8")
    public Object buildlist(HttpServletRequest request) {
        //CommonResponse commonResponse = CommonResponse.success();
        try {
            return  neighbourhoodService.buildlist(request);
        } catch (Exception e) {
            log.error("find admin users error:{}", e);

        }
        return null;
    }
    /**
     * 模糊查询小区列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param keyWord  模糊查询关键字
     * @return 小区信息分页对象
     */
    @ApiOperation(value = "跨行政区域查询小区信息", notes = "跨街道行政区域查询小区信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "keyWord", value = "查询关键字", required = false, dataType = "String")
    })
    @RequestMapping(value = "/api/neighbourhoods", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse findNeighbourhoodsByKeyWord(int pageSize, int pageNum, String keyWord) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(neighbourhoodService.findNeighbourhoods(pageNum, pageSize, keyWord));
        } catch (Exception e) {
            log.error("get neighbourhoods error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 为小区新增楼栋幢数
     *
     * @param building 楼栋信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增楼栋信息", notes = "新增楼栋信息")
    @ApiImplicitParam(name = "building", value = "楼栋信息实体", required = true, dataType = "Building")
    @RequiresPermissions("admin:neighbourhood:building:add")
    @PermissionReport(value = "admin:neighbourhood:building:add", name = "新增幢数", group = "admin_neighbourhood")
    @RequestMapping(value = "/admin/building", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse addBuilding(@RequestBody Building building) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            neighbourhoodService.addBuilding(building);
        } catch (Exception e) {
            log.error("add building error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除小区楼栋信息
     *
     * @param id 指定楼栋信息
     * @return 删除结果
     */
    @ApiOperation(value = "删除楼栋信息", notes = "删除楼栋信息")
    @ApiImplicitParam(name = "id", value = "楼栋信息id", required = true, dataType = "Long")
    @RequiresPermissions("admin:neighbourhood:building:delete")
    @PermissionReport(value = "admin:neighbourhood:building:delete", name = "删除幢数", group = "admin_neighbourhood")
    @RequestMapping(value = "/admin/building/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public CommonResponse deleteBuilding(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            neighbourhoodService.deleteBuilding(id);
        } catch (Exception e) {
            log.error("delete building error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 查询指定小区内的楼栋信息
     *
     * @param pageNum       页码
     * @param pageSize      每页数据量
     * @param neighbourhood 小区id
     * @return 小区内楼栋信息
     */
    @ApiOperation(value = "查询小区信息楼栋信息", notes = "查询小区楼栋信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "neighbourhood", value = "小区id", required = true, dataType = "long")
    })
    @RequestMapping(value = "/api/buildings", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse findBuildings(int pageNum, int pageSize, long neighbourhood) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(neighbourhoodService.findBuildings(pageNum, pageSize, neighbourhood).getList());
        } catch (Exception e) {
            log.error("get buildings error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 为楼栋增加户号门牌号
     *
     * @param household 门牌号信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增门牌号信息", notes = "新增门牌号信息")
    @ApiImplicitParam(name = "household", value = "门牌号实体", required = true, dataType = "HouseHold")
    @RequiresPermissions("admin:neighbourhood:household:add")
    @PermissionReport(value = "admin:neighbourhood:household:add", name = "新增门牌号", group = "admin_neighbourhood")
    @RequestMapping(value = "/admin/household", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse addHouseHold(@RequestBody Household household) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            neighbourhoodService.addHouseHold(household);
        } catch (Exception e) {
            log.error("add household error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除楼栋门牌号信息
     *
     * @param id 指定门牌号信息
     * @return 删除结果
     */
    @ApiOperation(value = "删除门牌号信息", notes = "删除门牌号信息")
    @ApiImplicitParam(name = "id", value = "门牌号id", required = true, dataType = "Long")
    @RequiresPermissions("admin:neighbourhood:household:delete")
    @PermissionReport(value = "admin:neighbourhood:household:delete", name = "删除门牌号", group = "admin_neighbourhood")
    @RequestMapping(value = "/admin/household/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public CommonResponse deleteHousehold(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            neighbourhoodService.deleteHouseHold(id);
        } catch (Exception e) {
            log.error("delete household error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 查询小区指定楼栋的门牌号信息
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param building 楼栋id
     * @return 门牌号信息
     */
    @ApiOperation(value = "根据楼栋查询门牌号信息", notes = "根据楼栋查询门牌号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "building", value = "楼栋id", required = true, dataType = "long")
    })
    @RequestMapping(value = "/api/households", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
        public CommonResponse findHouseHolds(int pageNum, int pageSize, long building) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(neighbourhoodService.findHouseHolds(pageNum, pageSize, building).getList());
        } catch (Exception e) {
            log.error("get households error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;

    }

    /**
     * 根据code查询门牌号信息，用作扫码时的第一步二维码有效性校验
     *
     * @param qrcode 二维码code
     * @return 二维码对应的门牌号信息
     */
    @ApiOperation(value = "根据code查询门牌号信息", notes = "用作扫码时的第一步二维码有效性校验")
    @ApiImplicitParam(name = "qrcode", value = "二维码", required = true, dataType = "String")
    @GetMapping(value = "/api/household/{qrcode}", produces = "application/json;charset=UTF-8")
    public CommonResponse findHouseholdByCode(@PathVariable(name = "qrcode") String qrcode) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            commonResponse.setData(neighbourhoodService.findHouseholdByCode(qrcode));
        } catch (Exception e) {
            log.error("find household by code error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

}
